<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传资源 - 学习资源平台</title>
    <link rel="stylesheet" th:href="@{/css/common.css}">
    <link rel="stylesheet" th:href="@{/css/navbar.css}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" th:href="@{/css/resourceupload.css}">
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg">
    <div class="container">
        <a class="navbar-brand" href="#">云学堂</a>
        <div class="collapse navbar-collapse">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                    <a class="nav-link active" th:href="@{/resource/list}">学习资源</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/assignment/list}">作业与考试</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/forum/list}">交流互动</a>
                </li>
                <li class="nav-item" th:if="${session.identity=='superAdmin' || session.identity=='ordinaryAdmin'}">
                    <a class="nav-link" th:href="@{/admin/index}">管理中心</a>
                </li>
            </ul>
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/user/digital}">个人中心</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/user/logout}">退出</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
<!-- 主内容 -->
<div class="container">
    <div class="upload-container">
        <h2 class="page-title">
            <i class="fas fa-cloud-upload-alt me-2"></i>上传学习资源
        </h2>

        <form action="#" th:action="@{/resource/upload}" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="title" class="form-label">资源标题</label>
                <input type="text" class="form-control" id="title" name="title" placeholder="请输入资源标题" required>
            </div>

            <div class="form-group">
                <label for="description" class="form-label">资源描述</label>
                <textarea class="form-control" id="description" name="description" rows="3" placeholder="请简要描述资源内容"></textarea>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="majorId" class="form-label">所属专业</label>
                        <select class="form-select" id="majorId" name="majorId" required>
                            <option value="">请选择专业</option>
                            <option value="1">计算机科学与技术</option>
                            <option value="2">软件工程</option>
                            <option value="3">人工智能</option>
                            <option value="4">数据科学与大数据技术</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="type" class="form-label">课程类型</label>
                        <select class="form-select" id="type" name="type" required>
                            <option value="必修">必修</option>
                            <option value="选修">选修</option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="resourceType" class="form-label">资源类型</label>
                <select class="form-select" id="resourceType" name="resourceType" required>
                    <option value="文档">文档</option>
                    <option value="视频">视频</option>
                    <option value="音频">音频</option>
                    <option value="其他">其他</option>
                </select>
            </div>

            <div class="form-group">
                <label for="file" class="form-label">选择文件</label>
                <div class="form-control-file text-center p-4" id="fileDropArea">
                    <i class="upload-icon fas fa-file-upload"></i>
                    <p>拖放文件到这里或点击上传</p>
                    <input type="file" class="d-none" id="file" name="file" required>
                    <button type="button" class="btn btn-outline-primary mt-2" id="browseBtn">浏览文件</button>
                </div>
                <div class="file-info" id="fileInfo">
                    <p><strong>已选择文件:</strong> <span id="fileName">未选择文件</span></p>
                    <p><strong>文件大小:</strong> <span id="fileSize">-</span></p>
                </div>
            </div>
            <div th:if="${message != null}" class="alert alert-info">
                <p th:text="${message}"></p>
            </div>
            <div class="d-flex gap-3 mt-4">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-upload me-2"></i>上传资源
                </button>
                <a th:href="@{/resource/list}" class="btn btn-secondary">
                    <i class="fas fa-times me-2"></i>取消
                </a>
            </div>
        </form>
    </div>

    <div class="footer">
        <p>© 2023 学习资源平台 | 让知识触手可及</p>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const fileInput = document.getElementById('file');
        const fileDropArea = document.getElementById('fileDropArea');
        const browseBtn = document.getElementById('browseBtn');
        const fileInfo = document.getElementById('fileInfo');
        const fileName = document.getElementById('fileName');
        const fileSize = document.getElementById('fileSize');
        // 点击浏览按钮触发文件选择
        browseBtn.addEventListener('click', function() {
            fileInput.click();
        });
        // 文件选择变化时更新信息
        fileInput.addEventListener('change', function() {
            if (this.files.length > 0) {
                updateFileInfo(this.files[0]);
            }
        });

        // 拖放功能
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            fileDropArea.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        ['dragenter', 'dragover'].forEach(eventName => {
            fileDropArea.addEventListener(eventName, highlight, false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            fileDropArea.addEventListener(eventName, unhighlight, false);
        });

        function highlight() {
            fileDropArea.style.borderColor = '#4361ee';
            fileDropArea.style.backgroundColor = 'rgba(67, 97, 238, 0.05)';
        }

        function unhighlight() {
            fileDropArea.style.borderColor = '#e1e5ee';
            fileDropArea.style.backgroundColor = '';
        }

        fileDropArea.addEventListener('drop', handleDrop, false);

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;

            if (files.length > 0) {
                fileInput.files = files;
                updateFileInfo(files[0]);
            }
        }

        function updateFileInfo(file) {
            fileName.textContent = file.name;
            // 格式化文件大小
            const fileSizeInBytes = file.size;
            let size = '';
            if (fileSizeInBytes >= 1073741824) {
                size = (fileSizeInBytes / 1073741824).toFixed(2) + ' GB';
            } else if (fileSizeInBytes >= 1048576) {
                size = (fileSizeInBytes / 1048576).toFixed(2) + ' MB';
            } else if (fileSizeInBytes >= 1024) {
                size = (fileSizeInBytes / 1024).toFixed(2) + ' KB';
            } else {
                size = fileSizeInBytes + ' bytes';
            }
            fileSize.textContent = size;
            fileInfo.classList.add('show');
        }
    });
</script>
</body>
</html>